<?php

namespace App\Manage;


use App\Models\Order;
use App\Models\SiteInfo;
use App\Service\Base\Manage;
use Illuminate\Support\Facades\Log;

class OrderManage extends Manage
{

    // 基本配置
    public function initialize()
    {
        parent::initialize();

        $this->model = 'Order';
    }

    // 列表
    // 添加
    // 修改
    // 软删除

    // 支付
    public function onPay()
    {
        $opt = $this->option();

        // 执行支付操作


        // 操作成功
        return true;
    }

    // 审核
    // 退款：审核操作
    public function onVerify()
    {
        //   如果审核通过后直接提现到用户
        $opt    = $this->option();
        $_value = $opt->getData('value', null, 'intval');
        $_id    = $opt->getData('id', 0, 'intval');
        if(is_null($_value)){
            return $this->_returnFail('状态必须填写！！');
        }
        if(empty($_id)){
            return $this->_returnFail('请指定操作记录！！');
        }
        if(!in_array($_value, [1, 2])){
            return $this->_returnFail('审核状态必须为2或1！！');
        }

        // 查询订单信息
        $orderDb = new Order();
        $order   = $orderDb->where('id', $_id)->first();
        if(empty($order)){
            return $this->_returnFail('订单不存在！！');
        }

        // 操作成功
        $result = $orderDb->where('id', $_id)->update(['verify' => $_value]);
        if($result === false){
            return $this->_returnFail($orderDb->getError());
        }

        // 删除缓存
        $orderDb->deleteCacheShow($order);


        // 执行退款操作
        if($_value == 1){
            //return $this->_returnFail('微信提现失败！！');

            $payV3  = new MiniPayLogicV3();
            $result = $payV3->refundPay($order['order_no'], $order['money']);

            // 删除缓存
            $orderDb->deleteCacheShow($order);
            if($result === false){
                // 取消审核
                $this->cancelVerify($_id);

                return $this->_returnFail($payV3->getError());
            }

            Log::info('退款:return'.json_encode($result, JSON_UNESCAPED_UNICODE));
            if(!empty($result['code']) && ($result['code'] != 200)){
                // 取消审核
                $this->cancelVerify($_id);

                return $this->_returnFail($result['message'] ?? '退款操作失败！');
            }
        }

        return $result;
    }

    // 取消审核
    protected function cancelVerify($_id)
    {
        return (new Order())->where('id', $_id)->update(['verify' => 0]);
    }

    // 退款：确认操作
    public function confirmRefund()
    {
        $opt = $this->option();

        // 执行退款操作

        // 操作成功
        return true;
    }

    // 列表查询
    protected function listWhere()
    {
        $option   = $this->option();
        $where    = [];
        $bang     = [];
        $uid      = $option->getData('uid', 0, 'intval');
        $e_id     = $option->getData('event_id', 0, 'intval');
        $s_time   = $option->getData('start_time', 0, 'strtotime');
        $e_time   = $option->getData('end_time', 0, 'strtotime');
        $orderNo  = $option->getData('order_no', null, 'trim');
        $name     = $option->getData('name', null, 'trim');
        $keyword  = $option->getData('keyword', null, 'trim');
        $isRefund = $option->getData('is_refund', null, 'trim');
        $gRefund  = $option->getData('get_refund', null, 'trim');
        if($uid){
            $bang[]  = 'uid';
            $where[] = ['uid', '=', $uid];
        }
        if($e_id){
            $bang[]  = 'event_id';
            $where[] = ['event_id', '=', $e_id];
        }
        if($s_time){
            $bang[]  = 'ctime';
            $where[] = ['ctime', '>=', $s_time];
        }
        if($e_time){
            $bang[]  = 'ctime';
            $where[] = ['ctime', '<=', $e_time];
        }
        if($isRefund){
            $bang[]  = 'is_refund';
            $where[] = ['is_refund', '=', $isRefund];
        }
        if($name){
            $bang    = false; // 设置不在缓存
            $where[] = ['name', 'like', "%".$name."%"];
        }
        if($orderNo){
            $bang    = false; // 设置不在缓存
            $where[] = ['order_no', 'like', "%".$orderNo."%"];
        }
        if($keyword){
            $bang = false; // 设置不在缓存
            //$where[] = $this->orWhere('username|nickname|mobile', 'like', $keyword);
        }
        if(!is_null($gRefund)){
            $bang = false; // 设置不在缓存
            if($gRefund){
                $where[] = ['old_id', '>', 0];
            } else{
                $where[] = ['old_id', '=', 0];
            }
        }

        // 缓存
        $this->bang = $bang;

        return $where;
    }


    // 添加之前
    protected function _before_add()
    {
        $opt = $this->option();
        $uid = $opt->getData("uid", null, "intval");
        if(empty($uid)){
            return $this->_returnFail('请指定用户');
        }

        $orNo = (new Order())->makeOrderNo($uid);
        $data = [
            //'id'        => $opt->getData("id", null, "intval"),
            'order_no'  => $orNo,
            'uid'       => $uid,
            'event_id'  => $opt->getData("event_id", null, "intval"),
            'number'    => $opt->getData("number", null, "intval"),
            'paytime'   => $opt->getData("paytime", LARAVEL_TIME, "intval"),
            'status'    => $opt->getData("status", 0, "intval"),
            'is_refund' => $opt->getData("is_refund", 0, "intval"),
            //'ctime'     => $opt->getData("ctime", null, "intval"),
            //'mtime'     => $opt->getData("mtime", null, "intval"),
        ];

        return $data;
    }

    // 修改之前
    protected function _before_edit()
    {
        $opt  = $this->option();
        $data = [
            //'id'        => $opt->getData("id", null, "intval"),
            //'order_no'  => $opt->getData("order_no", null, "trim"),
            'uid'       => $opt->getData("uid", null, "intval"),
            'event_id'  => $opt->getData("event_id", null, "intval"),
            'number'    => $opt->getData("number", null, "intval"),
            'paytime'   => $opt->getData("paytime", LARAVEL_TIME, "intval"),
            'status'    => $opt->getData("status", 0, "intval"),
            'is_refund' => $opt->getData("is_refund", 0, "intval"),
            //'ctime'     => $opt->getData("ctime", null, "intval"),
            //'mtime'     => $opt->getData("mtime", null, "intval"),
        ];

        return $data;
    }


}


